package org.boot.fast.util;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MappingUtil {

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ user_name → userName ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    public static List<Map<String, Object>> mappingToJava(List<Map<String, Object>> list) {
        if (list == null) {
            return null;
        }
        List<Map<String, Object>> result = new ArrayList<>();
        for (Map<String, Object> map : list) {
            result.add(mappingToJava(map));
        }
        return result;
    }

    public static Map<String, Object> mappingToJava(Map<String, Object> map) {
        if (map == null) {
            return null;
        }
        Map<String, Object> result = new LinkedHashMap<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            result.put(mappingToJava(entry.getKey()), entry.getValue());
        }
        return result;
    }

    public static String mappingToJava(String field) {
        if (StrUtil.isEmpty(field))  return field;
        StringBuilder sb = new StringBuilder();
        String[] parts = field.split("_");
        for (int i = 0; i < parts.length;  i++) {
            if (i == 0) {
                sb.append(parts[i]);
            } else {
                sb.append(Character.toUpperCase(parts[i].charAt(0)))
                        .append(parts[i].substring(1));
            }
        }
        return sb.toString();
    }

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ userName → user_name ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    public static List<Map<String, Object>> mappingToTable(List<Map<String, Object>> list) {
        if (list == null) {
            return null;
        }
        List<Map<String, Object>> result = new ArrayList<>();
        for (Map<String, Object> map : list) {
            result.add(mappingToTable(map));
        }
        return result;
    }

    public static Map<String, Object> mappingToTable(Map<String, Object> map) {
        if (map == null) {
            return null;
        }
        Map<String, Object> result = new LinkedHashMap<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            result.put(mappingToTable(entry.getKey()), entry.getValue());
        }
        return result;
    }

    public static String mappingToTable(String field) {
        if (StrUtil.isEmpty(field))  {
            return field;
        }
        // 首字母单独处理，后续字母按原逻辑转换
        String firstChar = field.substring(0,  1);
        String remaining = field.substring(1)
                .replaceAll("(?=[A-Z])", "_")
                .toLowerCase();
        return firstChar + remaining;
    }




















    /**
     * 下划线转小驼峰（user_name → userName）
     * @param field
     * @return
     */
    public static String mappingResponse(String field) {
        if (StrUtil.isEmpty(field))  return field;
        StringBuilder sb = new StringBuilder();
        String[] parts = field.split("_");
        for (int i = 0; i < parts.length;  i++) {
            if (i == 0) {
                sb.append(parts[i]);
            } else {
                sb.append(Character.toUpperCase(parts[i].charAt(0)))
                        .append(parts[i].substring(1));
            }
        }
        return sb.toString();
    }

    /**
     * 驼峰命名转下划线命名（userName → user_name）
     * @param field
     * @return
     */
    public static String mappingJoinIn(String field) {
        if (StrUtil.isEmpty(field))  {
            return field;
        }
        // 首字母单独处理，后续字母按原逻辑转换
        String firstChar = field.substring(0,  1);
        String remaining = field.substring(1)
                .replaceAll("(?=[A-Z])", "_")
                .toLowerCase();
        return firstChar + remaining;
    }
    public static Map<String, Object> camelCaseMap(Map<String, Object> map) {
        if (map == null) {
            return null;
        }
        Map<String, Object> result = new LinkedHashMap<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            result.put(mappingJoinIn(entry.getKey()), entry.getValue());
        }
        return result;
    }


}
